<template>
  <div class="comment">
    <div class="header">
      <div class="total">留言总数:{{ total }}</div>
    </div>
    <el-table :data="tableData" height="calc(100% - 110px)" style="width: 100%">
      <el-table-column prop="content" label="留言内容" width="200px">
        <template slot-scope="scope">
          <div class="content">{{ scope.row.content }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="article_name" label="文章标题"></el-table-column>
      <el-table-column prop="real_name" label="称呼"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="create_time" label="创建时间">
        <template slot-scope="scope">{{
          scope.row.create_time
            ? dayjs(Number(scope.row.create_time)).format("YYYY-MM-DD HH:mm:ss")
            : "-"
        }}</template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">{{
          scope.row.status === 0 ? "隐藏" : "公开"
        }}</template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === 0"
            @click="handlePublish(scope.row)"
            type="primary"
            size="small"
            >公开</el-button
          >
          <el-button
            v-else
            @click="handleShield(scope.row)"
            type="primary"
            size="small"
            >隐藏</el-button
          >
          <el-button @click="openDelete(scope.row)" type="danger" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :page-size="pageSize"
      :current-page="page"
      @current-change="pageChange"
      layout="prev, pager, next"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import dayjs from "dayjs";
import { createNamespacedHelpers } from "vuex";
const { mapActions } = createNamespacedHelpers("admin");
export default {
  data() {
    return {
      pageSize: 10,
      page: 1,
      comment_id: 1,
      tableData: [],
      total: 0
    };
  },
  mounted() {
    this.fetchCommentList();
  },
  methods: {
    dayjs,
    ...mapActions([
      "getCommentlist",
      "deleteComment",
      "commentPublish",
      "commentShield"
    ]),
    openDelete(row) {
      this.$confirm("确认删除？")
        .then(() => {
          this.comment_id = row.comment_id;
          this.deleteClick();
        })
        .catch(() => {});
    },

    handlePublish(row) {
      const { comment_id } = row;
      this.commentPublish(comment_id).then(() => {
        this.fetchCommentList();
      });
    },
    handleShield(row) {
      const { comment_id } = row;
      this.commentShield(comment_id).then(() => {
        this.fetchCommentList();
      });
    },

    deleteClick() {
      this.deleteComment(this.comment_id).then(() => {
        this.fetchCommentList();
      });
    },
    fetchCommentList() {
      const params = {
        page: this.page,
        pageSize: this.pageSize,
        status: [0, 1]
      };
      this.getCommentlist(params).then(res => {
        this.tableData = res.list;
        this.total = res.total;
      });
    },
    pageChange(res) {
      this.page = res;
    }
  }
};
</script>

<style lang="scss" scoped>
.comment {
  height: 100%;
  .header {
    display: flex;
    align-items: center;
    height: 64px;
    overflow-x: auto;
  }
  .el-table {
    //   height: calc(100% - 110px);
    //   overflow: hidden;
    // /deep/  .el-table__body-wrapper {
    //     height: calc(100% - 48px);
    //     overflow: auto;
    //   }
    .content {
      width: 100%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
</style>
